package com.junbao.message.infrastructure.command;

import java.util.concurrent.atomic.AtomicInteger;

import com.junbao.message.domain.model.NotifyMessage;
import com.junbao.message.infrastructure.queue.WheelQueue;
import com.junbao.message.infrastructure.queue.message.ConfirmMessageTask;
import com.junbao.message.infrastructure.queue.message.MessageQueueBootStrap;
import com.junbao.message.infrastructure.queue.message.RetryMessageTask;

public class MessageReceiver {
	 
    private final NotifyMessage message;
 
    private AtomicInteger retryMessageTaskCount = new AtomicInteger();
    private AtomicInteger confirmMessageTaskCount = new AtomicInteger();
 
    private static MessageQueueBootStrap messageQueueBootStrap;
    private static WheelQueue messageQueue;
 
    static {
        messageQueueBootStrap = new MessageQueueBootStrap();
        messageQueue = messageQueueBootStrap.start();
    }
 
    public MessageReceiver(NotifyMessage message) {
        this.message = message;
 
    }
 
    public void messageRetry() {
        String id = "retry_message_task" + retryMessageTaskCount.incrementAndGet();
        if (message.meesageStatusIsDead()) {
            return;
        }
        RetryMessageTask messageTask = new RetryMessageTask(id, message);
        messageQueue.add(messageTask);
    }
 
    public void messageConfirm() {
        String id = "confirm_message_task" + confirmMessageTaskCount.incrementAndGet();
        if (message.meesageStatusIsDead()) {
            return;
        }
        ConfirmMessageTask messageTask = new ConfirmMessageTask(id, message);
        messageQueue.add(messageTask);
    }
}